//InnerAudioContext实例
//const app = getApp()
//InnerAudioContext实例
var audioCxt;
//动画
var audioAnimation;
audioCxt = wx.createInnerAudioContext();
audioCxt.src = 'https://zhimi-lake.oss-cn-beijing.aliyuncs.com/audio/kata.mp3';
 
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tips: {
      type: Array,
      value: [],
      observer: function (n) {
        if (n.length) {
          n.forEach(item => {
            if (item.type == 'warn') {
              item.w = item.tip_content.length * 40;
            } else {
              item.w = item.btn.length * 120;
            }

          })
          this.setData({
            tipsArr: n
          })
        }
      }
    },
    callbackFn: {
      type: Function,
      value: () => {}
    },
    tip0: {
      type: String,
      value: '',
      observer: function (n) {
        if (n.length) {
          this.setData({
            tip0: n
          })
        }
      }
    },
    tip1: {
      type: String,
      value: '',
      observer: function (n) {
        if (n.length) {
          this.setData({
            tip1: n
          })
        }
      }
    },
    tip2: {
      type: String,
      value: '',
      observer: function (n) {
        if (n.length) {
          this.setData({
            tip2: n
          })
        }
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    x1: 244,
    y1: 32,
    x2: 102,
    y2: 16,
    x3: 179,
    y3: 60,
    x4: 311,
    y4: 12,
    x5: 244,
    y5: 82,
    x6: 102,
    y6: 66,
    x7: 179,
    y7: 110,
    x8: 311,
    y8: 62,
    x9: 209,
    y9: 210,
    x10: 281,
    y10: 162,
    
    x1ok: 25,
    y1ok: 27,
    x2ok: 195,
    y2ok: 48,
    x3ok: 335,
    y3ok: 0,
    x4ok: 127,
    y4ok: 50,
    scale: 20,
    isok:'',//2413
    play1:0,
    play2:0,
    play3:0,
    play4:0,
    ok1:0,
    ok2:0,
    ok3:0,
    ok4:0,
  },
  /**
  * 页面加载完成执行
  */
 lifetimes:{
  ready(){
    var that =this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight:  res.windowHeight,
          windowWidth:  res.windowWidth,
          //buttonTop1:buttonTop1,//这里定义按钮的初始位置
          //buttonLeft:res.windowWidth*0.70,//这里定义按钮的初始位置
        })
      }
    })
    

  },
  attached: function() {
    // 在组件实例进入页面节点树时执行
  },
  detached: function() {
    // 在组件实例被从页面节点树移除时执行
  },
},
 //组件所在页面的生命周期
 pageLifetimes: {
  show: function() {
    // 页面被展示
  },
  hide: function() {
    // 页面被隐藏
  },
  resize: function(size) {
    // 页面尺寸变化
  }
},
   /**
   * 组件的方法列表
   */
  methods: {
  //以下是按钮拖动事件
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
    //在播放状态，绑定播放进度更新事件。然后控制进度条和时间显示
    audioCxt.onPlay((res) =>{ 
      //audioCxt.onTimeUpdate(this.timeUpdate)
      console.log('播放start')
    }),
    //音乐播放结束触发
    audioCxt.onEnded((res)=>{
      audioCxt.seek(0);
      console.log('播放end')
    })
  },
 
  tap1(e) {
    console.log('x==='+e.detail.x+' y==='+e.detail.y +' source==='+ e.detail.source);
    let x1 = e.detail.x;
    let y1 = e.detail.y;
    let x1ok = this.data.x1ok;
    let y1ok = this.data.y1ok;
    let scale = this.data.scale;
    //console.log(e.target.dataset.play)
    
    if((x1 >= x1ok-scale && x1 <= x1ok+scale) && (y1 >= y1ok-scale && y1 <= y1ok+scale))
    {
      let isok = this.data.isok+1+',';
      console.log('lock1 ok')
      wx.vibrateLong();
      // this.setData({
      //   x1: e.detail.x,
      //   y1: e.detail.y,
      //   isok:isok,
      //   ok1:1,
      // })
    }
    if(this.data.play1 == 0){
      this.playMusic();
    }
    this.setData({
      play1:1,
    })
    
  },
  tap2(e) {
    console.log('x==='+e.detail.x+' y==='+e.detail.y);
    let x1 = e.detail.x;
    let y1 = e.detail.y;
    let x1ok = this.data.x2ok;
    let y1ok = this.data.y2ok;
    let scale = this.data.scale;  
    
    if((x1 >= x1ok-scale && x1 <= x1ok+scale) && (y1 >= y1ok-scale && y1 <= y1ok+scale))
    {
      let isok = this.data.isok+2+',';
      console.log('lock2 ok')
      wx.vibrateLong();
      this.setData({
        x2: e.detail.x,
        y2: e.detail.y,
        isok:isok,
        ok2:1,
      })
    }
    if(this.data.play2 == 0){
      this.playMusic();
    }
    this.setData({
      play2:1,
    })
    
  },
  tap3(e) {
    console.log('x==='+e.detail.x+' y==='+e.detail.y);
    let x1 = e.detail.x;
    let y1 = e.detail.y;
    let x1ok = this.data.x3ok;
    let y1ok = this.data.y3ok;
    let scale = this.data.scale;
    
    if((x1 >= x1ok-scale && x1 <= x1ok+scale) && (y1 >= y1ok-scale && y1 <= y1ok+scale))
    {
      let isok = this.data.isok+3+',';
      console.log('lock3 ok')
      wx.vibrateLong();
      this.setData({
        x3: e.detail.x,
        y3: e.detail.y,
        isok:isok,
        ok3:1,
      })
    }
    if(this.data.play3 == 0){
      this.playMusic();
    }
    this.setData({
      play3:1,
    })
    
  },
  tap4(e) {
    console.log('x==='+e.detail.x+' y==='+e.detail.y);
    let x1 = e.detail.x;
    let y1 = e.detail.y;
    let x1ok = this.data.x4ok;
    let y1ok = this.data.y4ok;
    let scale = this.data.scale;
    
    if((x1 >= x1ok-scale && x1 <= x1ok+scale) && (y1 >= y1ok-scale && y1 <= y1ok+scale))
    {
      let isok = this.data.isok+4+',';
      console.log('lock4 ok')
      wx.vibrateLong();
      this.setData({
        x4: e.detail.x,
        y4: e.detail.y,
        isok:isok,
        ok4:1,
      })
    }
    if(this.data.play4 == 0){
      this.playMusic();
    }
    this.setData({
      play4:1,
    })
    
  },
  resetKey(){
    this.setData({
    //   x1: 244,
    // y1: 32,
    // x2: 102,
    // y2: 16,
    // x3: 179,
    // y3: 60,
    // x4: 311,
    // y4: 12,
      isok:'',
      play1:0,
    play2:0,
    play3:0,
    play4:0,
    })
    wx.redirectTo({
      url: '../lock/test4',
    })
  },
  isOk(){
    let isok = this.data.isok;
    var words = isok.split(",");
    words = this.uniqueStr(words);
    isok = words.join(',');
    let tip = '再想想!';
    if(isok == '2,4,1,3,'){
      console.log('锁开了!')
      tip = '锁开了!';
    }
    else{
      console.log('2413再想想!'+ isok)
    }
    wx.showModal({
      title: '提示',
      content: tip,
      success: (res) => {
          // wx.switchTab({
          //   url: '/pages/strategyList/strategyList'
          // })                
      }
    })
  },
    //播放按钮事件
    playMusic : function(){
      audioCxt.play();
    },
  uniqueStr (array){
    return array.filter(function(el, index, arr) {
    return index == arr.indexOf(el);
    });
  },
  }
})